package com.dsg.scheduler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dsg.common.util.FileNotCompleteException;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.common.util.toolbox.DateUtil;
import com.dsg.entity.apply.DsgApplyBill;
import com.dsg.entity.apply.DsgApplyCar;
import com.dsg.entity.apply.DsgApplyCard;
import com.dsg.entity.apply.DsgApplyDiscount;
import com.dsg.entity.approve.DsgApproveCase;
import com.dsg.service.apply.*;
import com.dsg.service.approve.IDsgApproveCaseService;
import com.dsg.service.approve.IDsgApproveWaittimeService;
import com.dsg.service.system.IDsgSysFileService;
import com.dsg.workflow.WorkflowEngine;
import com.dsg.workflow.WorkflowService3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@Slf4j
public class CheckFileCompleteTask {
    @Autowired
    WorkflowEngine engine;
    @Autowired
    IDsgApproveCaseService caseService;
    @Autowired
    IDsgApproveWaittimeService waittimeService;
    @Autowired
    IDsgApplyCarService carService;
    @Autowired
    IDsgApplyPayService payService;
    @Autowired
    IDsgSysFileService fileService;
    @Autowired
    IDsgApplyBillService billService;
    @Autowired
    IDsgApplyCardService cardService;
    @Autowired
    IDsgApplyDiscountService discountService;

    /**
     * iPad返單跌落 Sales Waiting List，如果超時沒有補齊資料，自動跌落 CS Waiting List
     */
    @Scheduled(fixedRate = 1000 * 60)
    public void method1() throws Exception {
        List<DsgApproveCase> caseList = caseService.timeoutCaseList();
        if (CollectionUtil.isNotEmpty(caseList)) {
            Set<String> appNos = caseList.stream().map(e -> e.getAppNo()).collect(Collectors.toSet());
            log.info("超时未提交共{}", appNos.size());
            List<String> appNoList = new ArrayList<>();
            for (int i = 0; i < caseList.size(); i++) {
                DsgApproveCase approveCase = caseList.get(i);

                //同一個單有多個開始CASE，只處理一個CASE
                String appNo = approveCase.getAppNo();
                if (appNoList.contains(appNo)) {
                    continue;
                } else {
                    appNoList.add(appNo);
                }

                try {
                    engine.fileComplete(approveCase);
                } catch (FileNotCompleteException e) {
                    log.info("資料不完整，自動跌落 CS Waiting List");
                    engine.forward(approveCase);
                }
            }
        }
    }

    /**
     * CS入機生成上級審批CASE
     * @throws Exception
     */
//    @Scheduled(fixedRate = 1000 * 60 * 1)
    public void method2() throws Exception {
        List<DsgApplyBill> billList = billService.selectApproveList();
        for (DsgApplyBill bill : billList) {
            engine.genSupApproveCase(WorkflowService3.NODE_SUP_APPROVE, bill);
        }
    }

    /**
     * 刪除過時沒保存的CS加車申請單
     */
//    @Scheduled(fixedRate = 1000 * 60 * 1)
    @Scheduled(cron = "0 0 0 * * ?")
    public void method3() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("APP_TYPE", DsgApplyBill.APP_TYPE_7);
        wrapper.eq("IS_SAVE", DsgApplyBill.IS_SAVE_N);
        Date date = DateUtil.addHours(new Date(), -1);
        wrapper.le("CREATE_TIME", date);
        List<DsgApplyBill> billList = billService.list(wrapper);
        if (CollectionUtil.isNotEmpty(billList)) {
            billService.remove(wrapper);
            for (DsgApplyBill bill : billList) {
                wrapper = new QueryWrapper();
                wrapper.eq("APP_NO", bill.getAppNo());
                fileService.remove(wrapper);
                List<DsgApplyCar> carList = carService.list(wrapper);
                if (CollectionUtil.isNotEmpty(carList)) {
                    carService.remove(wrapper);
                    for (DsgApplyCar car : carList) {
                        wrapper = new QueryWrapper();
                        wrapper.eq("CAR_ID", car.getOid());
                        List<DsgApplyCard> cardList = cardService.list(wrapper);
                        if (CollectionUtil.isNotEmpty(cardList)) {
                            cardService.remove(wrapper);
                            for (DsgApplyCard card : cardList) {
                                wrapper = new QueryWrapper();
                                wrapper.eq("DIS_TARGET", DsgApplyDiscount.DIS_TARGET_CARD);
                                wrapper.eq("CARD_ID", card.getOid());
                                discountService.remove(wrapper);
                            }
                        }
                    }
                }
            }
        }
    }
}
